//
//  SASaleDocumentsImageHeaderView.m
//  saasRetail
//
//  Created by 黄志刚 on 2022/3/23.
//

#import "SASaleDocumentsImageHeaderView.h"
#import "SASalesInvoiceDetailModel.h"
#import "SASalesOrderViewListModel.h"

@interface SASaleDocumentsImageHeaderView ()

@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *colorSizeLab;
@property (nonatomic, strong) UILabel *priceLab;

@property (nonatomic, strong) UILabel *discountPriceLab;
@property (nonatomic, strong) UILabel *percentageLab;
@property (nonatomic, strong) UILabel *totalPriceLab;
@property (nonatomic, strong) UILabel *returnLab;

@end

@implementation SASaleDocumentsImageHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self initUI];
    }
    return self;
}

-(void)setModel:(SAProductListModel *)model {
    _model = model;
    self.titleLab.text = [NSString stringWithFormat:@"%@,%@",model.productCode,model.productName];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"sale_bule_defaul"]];
    if (model.skuList.count > 0) {
        SASaleSkuListModel *skuList = model.skuList[0];
        self.colorSizeLab.text = [NSString stringWithFormat:@"%@,%@",skuList.colourName,skuList.sizeName];
        NSString *price = [NSString stringWithFormat:@"%0.2f",[skuList.price floatValue]/100.f].getEffectivePointString;
        self.priceLab.text = [NSString stringWithFormat:@"单价:%@*%@",price,skuList.count];
        
        CGFloat discountRate = [skuList.discountRate floatValue]/100;
        if (discountRate == 100) {
            self.totalPriceLab.frame = SARectMake(211, 0, 75, 84);
            self.percentageLab.text = @"";
            NSString *textContent = [NSString stringWithFormat:@"%0.2f",[skuList.totalPrice floatValue]/100.f].getEffectivePointString;
            self.totalPriceLab.attributedText = [[NSAttributedString alloc] initWithString:textContent attributes:nil];
        }else {
            NSString *percentageStr = [NSString stringWithFormat:@"%0.2f",discountRate].getEffectivePointString;
            self.percentageLab.text = [NSString stringWithFormat:@"%@%%",percentageStr];
            self.totalPriceLab.frame = SARectMake(211, 43, 75, 20);
            NSString *textContent = [NSString stringWithFormat:@"%0.2f",[skuList.totalPrice floatValue]/100.f].getEffectivePointString;
            NSMutableDictionary *textDict = [NSMutableDictionary dictionary];
            textDict[NSStrikethroughColorAttributeName] = kPinkColor;
            textDict[NSForegroundColorAttributeName] = kBlackColor;
            textDict[NSFontAttributeName] = kFont(14);
            textDict[NSStrikethroughStyleAttributeName] = [NSNumber numberWithInteger:NSUnderlineStyleSingle];
            textDict[NSBaselineOffsetAttributeName] = @(NSUnderlineStyleSingle);
            self.totalPriceLab.attributedText = [[NSAttributedString alloc] initWithString:textContent attributes:textDict];
        }
        
        self.discountPriceLab.text = [NSString stringWithFormat:@"%0.2f",[skuList.discountPrice floatValue]/100.f].getEffectivePointString;
        NSInteger returnCount = [skuList.returnCount integerValue];
        if (returnCount > 0) {
            self.returnLab.hidden = NO;
            self.discountPriceLab.frame = SARectMake(287, 24, 88, 18);
            if (returnCount < [skuList.count integerValue]) {
                self.returnLab.text = @"部分退";
            }else {
                self.returnLab.text = @"已退款";
            }
        }else {
            self.returnLab.hidden = YES;
            self.discountPriceLab.frame = SARectMake(287, 0, 88, 84);
        }
        
        if ([skuList.discountPrice floatValue] < 0) {
            self.discountPriceLab.textColor = kPinkColor;
        }else {
            self.discountPriceLab.textColor = kBlackColor;
        }
    }
}

-(void)setEnterModel:(SASalesOrderViewListModel *)enterModel {
    _enterModel = enterModel;
    self.titleLab.frame = SARectMake(80, 10, 200, 20);
    self.colorSizeLab.frame = SARectMake(80, 32, 200, 20);
    self.priceLab.frame = SARectMake(80, 54, 200, 20);
    self.returnLab.hidden = YES;

    self.titleLab.text = [NSString stringWithFormat:@"%@,%@",enterModel.productCode,enterModel.productName];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:enterModel.img] placeholderImage:[UIImage imageNamed:@"sale_bule_defaul"]];
    if (enterModel.toPendSkuAttrs.count > 0) {
        SASalesOrderToPendSkuList *skuList = enterModel.toPendSkuAttrs[0];
        self.colorSizeLab.text = [NSString stringWithFormat:@"%@,%@",skuList.colourName,skuList.sizeName];
        NSString *price = [NSString stringWithFormat:@"%0.2f",[skuList.price floatValue]/100.f].getEffectivePointString;
        self.priceLab.text = [NSString stringWithFormat:@"单价:%@,数量:%@",price,skuList.count];
        self.discountPriceLab.text = [NSString stringWithFormat:@"%0.2lf",[skuList.subtotal doubleValue]/100.f].getEffectivePointString;
        
        if ([skuList.subtotal floatValue] < 0) {
            self.discountPriceLab.textColor = kPinkColor;
        }else {
            self.discountPriceLab.textColor = kBlackColor;
        }
    }
}

-(void)setInventoryModel:(SASalesOrderViewListModel *)inventoryModel {
    _inventoryModel = inventoryModel;
    self.titleLab.frame = SARectMake(80, 10, 200, 20);
    self.colorSizeLab.frame = SARectMake(80, 32, 200, 20);
    self.priceLab.frame = SARectMake(80, 54, 200, 20);
    self.returnLab.hidden = YES;

    self.titleLab.text = [NSString stringWithFormat:@"%@,%@",inventoryModel.productCode,inventoryModel.productName];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:inventoryModel.img] placeholderImage:[UIImage imageNamed:@"sale_bule_defaul"]];
    if (inventoryModel.toPendSkuAttrs.count > 0) {
        SASalesOrderToPendSkuList *skuList = inventoryModel.toPendSkuAttrs[0];
        self.colorSizeLab.text = [NSString stringWithFormat:@"%@,%@",skuList.colourName,skuList.sizeName];
        self.priceLab.text = [NSString stringWithFormat:@"盘前:%@,盘后:%@",skuList.count,skuList.stock];
        self.discountPriceLab.text = skuList.subtotal;
    }
}

-(void)lookImage {
    !self.lookImageBlock ?: self.lookImageBlock();
}

-(void)initUI {
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.imageV];
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.colorSizeLab];
    [self.contentView addSubview:self.priceLab];
    [self.contentView addSubview:self.discountPriceLab];
    [self.contentView addSubview:self.percentageLab];
    [self.contentView addSubview:self.totalPriceLab];
    [self.contentView addSubview:self.returnLab];
}

-(UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] initWithFrame:SARectMake(20, 10, 53, 64)];
        _imageV.image = [UIImage imageNamed:@"sale_bule_defaul"];
        _imageV.contentMode = UIViewContentModeScaleAspectFit;
        _imageV.userInteractionEnabled = YES;
        UITapGestureRecognizer *imageVlTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lookImage)];
        [_imageV addGestureRecognizer:imageVlTap];
    }
    return _imageV;
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(80, 10, 150, 20)];
        _titleLab.textColor = kBlackColor;
        _titleLab.font = kFont(14);
        _titleLab.adjustsFontSizeToFitWidth = YES;
        _titleLab.minimumScaleFactor = 0.8;
    }
    return _titleLab;
}

-(UILabel *)colorSizeLab {
    if (!_colorSizeLab) {
        _colorSizeLab = [[UILabel alloc] initWithFrame:SARectMake(80, 32, 130, 20)];
        _colorSizeLab.textColor = kLightgGrayColor;
        _colorSizeLab.font = kFont(14);
    }
    return _colorSizeLab;
}

-(UILabel *)priceLab {
    if (!_priceLab) {
        _priceLab = [[UILabel alloc] initWithFrame:SARectMake(80, 54, 150, 20)];
        _priceLab.textColor = kLightgGrayColor;
        _priceLab.font = kFont(14);
        _priceLab.adjustsFontSizeToFitWidth = YES;
        _priceLab.minimumScaleFactor = 0.8;
    }
    return _priceLab;
}

-(UILabel *)percentageLab {
    if (!_percentageLab) {
        _percentageLab = [[UILabel alloc] initWithFrame:SARectMake(211, 24, 75, 18)];
        _percentageLab.textColor = kPinkColor;
        _percentageLab.font = kFont(14);
        _percentageLab.textAlignment = NSTextAlignmentCenter;
    }
    return _percentageLab;
}

-(UILabel *)totalPriceLab {
    if (!_totalPriceLab) {
        _totalPriceLab = [[UILabel alloc] initWithFrame:SARectMake(211, 0, 75, 84)];
        _totalPriceLab.textColor = kBlackColor;
        _totalPriceLab.font = kFont(14);
        _totalPriceLab.textAlignment = NSTextAlignmentCenter;
    }
    return _totalPriceLab;
}

-(UILabel *)discountPriceLab {
    if (!_discountPriceLab) {
        _discountPriceLab = [[UILabel alloc] initWithFrame:SARectMake(287, 0, 88, 84)];
        _discountPriceLab.textColor = kBlackColor;
        _discountPriceLab.font = kFont(14);
        _discountPriceLab.textAlignment = NSTextAlignmentCenter;
    }
    return _discountPriceLab;
}


-(UILabel *)returnLab {
    if (!_returnLab) {
        _returnLab = [[UILabel alloc] initWithFrame:SARectMake(287, 42, 88, 18)];
        _returnLab.textColor = kPinkColor;
        _returnLab.font = kFont(13);
        _returnLab.textAlignment = NSTextAlignmentCenter;
        _returnLab.text = @"已退款";
    }
    return _returnLab;
}


@end
